package com.shell.provider;

import com.shell.bee.base.message.keyword.BaseKeyword;
import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.governor.modules.util.MessageSender;
import com.shell.sms.SMSResponse;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PubMessageProvider {
	@RequestMapping(value = { "/message/sendMessageToPhone" }, method = { RequestMethod.POST })
	SMSResponse sendMessageToPhone(@RequestParam("contactorPhone") String contactorPhone,
			@RequestParam("userId") String userId, @RequestParam("template") String template,
			@RequestParam("keyWord") String keyWordJson, @RequestParam("keyWordClassName") String keyWordClassName) {
		BaseKeyword keyword = getKeyword(keyWordJson, keyWordClassName);
		return MessageSender.sendMessageToPhone(contactorPhone, userId, template, keyword);
	}

	@RequestMapping(value = { "/message/sendMessageByRoleCode" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	public void sendMessageByRoleCode(@RequestParam String roleCodes, @RequestParam("senderId") String senderId,
			@RequestParam("template") String template, @RequestParam("keyWord") String keyWordJson,
			@RequestParam("keyWordClassName") String keyWordClassName,
			@RequestParam("receiverEntId") String receiverEntId) {
		String[] roleCodeArr = null;
		if (StringUtil.notEmpty(roleCodes)) {
			roleCodeArr = roleCodes.split(",");
		}
		BaseKeyword keyword = getKeyword(keyWordJson, keyWordClassName);
		MessageSender.sendMessageByRoleCode(roleCodeArr, senderId, template, keyword, receiverEntId);
	}

	@RequestMapping(value = { "/message/sendMessage" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	public void sendMessage(@RequestParam String userIds, @RequestParam("senderId") String senderId,
			@RequestParam("messageTemplateId") String messageTemplateId, @RequestParam("keyWord") String keyWordJson,
			@RequestParam("keyWordClassName") String keyWordClassName) {
		String[] userIdArr = null;
		if (StringUtil.notEmpty(userIds)) {
			userIdArr = userIds.split(",");
		}
		List<String> userIdList = Arrays.asList(userIdArr);
		BaseKeyword keyword = getKeyword(keyWordJson, keyWordClassName);
		MessageSender.sendMessage(userIdList, senderId, messageTemplateId, keyword);
	}

	@RequestMapping(value = { "/message/sendMessageByTmpl" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	public void sendMessageByTmpl(@RequestParam("senderId") String senderId,
			@RequestParam("template") String messageTemplateId, @RequestParam("keyWord") String keyWordJson,
			@RequestParam("keyWordClassName") String keyWordClassName,
			@RequestParam("receiverEntId") String receiverEntId) {
		BaseKeyword keyword = getKeyword(keyWordJson, keyWordClassName);
		MessageSender.sendMessageByTmpl(senderId, messageTemplateId, keyword, receiverEntId);
	}

	private BaseKeyword getKeyword(String keyWordJson, String keyWordClassName) {
		BaseKeyword keyword = new BaseKeyword();
		if ((StringUtil.notEmpty(keyWordJson)) && (StringUtil.notEmpty(keyWordClassName))) {
			try {
				Class<?> clazz = Class.forName(keyWordClassName);
				keyword = (BaseKeyword) JsonUtil.fromJson(keyWordJson, clazz);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return keyword;
	}
}
